package com.zline.foodraise.view;

import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zline.foodraise.R;
import com.zline.foodraise.constant.Constant;
import com.zline.foodraise.entity.CfOrderBean;
import com.zline.foodraise.entity.ShopBean;
import com.zline.foodraise.listener.RecyclerItemClicked;
import com.zline.foodraise.util.TimeUtil;

import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EViewGroup;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.res.StringRes;

/**
 * Created by heshantao on 15/12/25.
 * 筹店中订单Item
 */
@EViewGroup(R.layout.raise_shop_order_item)
public class RaiseShopOderItem extends RelativeLayout {
    private static final String LOG_TAG = RaiseShopOderItem.class.getSimpleName();
    Context cxt;
    int position;
    //订单编号
    @ViewById(R.id.raise_shop_order_item_order_code_value_tv)
    TextView order_code_tv;
    //订单状态
    @ViewById(R.id.raise_shop_order_item_payment_condition_tv)
    TextView order_status_tv;
    //订单生成时间
    @ViewById(R.id.raise_shop_order_item_order_code_create_time_tv)
    TextView order_create_time_tv;
    //订单金额
    @ViewById(R.id.raise_shop_order_item_order_amount_value_tv)
    TextView order_amount_tv;
    //退款状态RelativeLayout
    @ViewById(R.id.raise_shop_order_item_refund_status_rl)
    RelativeLayout order_refund_status_rl;
    //退款状态
    @ViewById(R.id.raise_shop_order_item_refund_status_value_tv)
    TextView order_refund_status_tv;
    //退款状态横线
    @ViewById(R.id.line2)
    View order_refund_status_line;
    //取货方式提示
    @ViewById(R.id.raise_shop_order_item_delivery_type_hint_tv)
    TextView order_delivery_type_hint_tv;
    //取货方式
    @ViewById(R.id.raise_shop_order_item_delivery_type_value_tv)
    TextView order_delivery_type_tv;
    //关联店铺
    @ViewById(R.id.raise_shop_order_item_related_store_value_tv)
    TextView order_relate_store_tv;
    //预约时间
    @ViewById(R.id.raise_shop_order_item_appointment_time_value_tv)
    TextView order_appointment_time_tv;
    //预约时间提示
    @ViewById(R.id.raise_shop_order_item_appointment_time_hint_tv)
    TextView order_appointment_time_hint_tv;
    //处理时间RelativeLayout
    @ViewById(R.id.raise_shop_order_item_handler_time_rl)
    RelativeLayout order_handler_time_rl;
    //处理时间
    @ViewById(R.id.raise_shop_order_item_handler_time_value_tv)
    TextView order_handler_time_tv;
    //处理时间横线
    @ViewById(R.id.line6)
    View order_handler_time_line;
    //用户名
    @ViewById(R.id.raise_shop_order_item_name_value_tv)
    TextView name_tv;
    //联系电话
    @ViewById(R.id.raise_shop_order_item_phone_value_tv)
    TextView phone_tv;
    //微信号
    @ViewById(R.id.raise_shop_order_item_wechate_value_tv)
    TextView wechate_number_tv;
    //收货地址
    @ViewById(R.id.raise_shop_order_item_addr_value_tv)
    TextView addr_tv;
    //标记处理按钮
    @ViewById(R.id.raise_shop_order_item_bottom_rl)
    RelativeLayout bottom_rl;
    @StringRes(R.string.today)
    String today;
    @Bean
    TimeUtil timeUtil;
    RecyclerItemClicked itemClicked;


    public RaiseShopOderItem(Context context) {
        super(context);
        this.cxt = context;
    }

    public void bind(CfOrderBean orderBean, int position,int flag) {
        this.position = position;
        if(orderBean!=null){
            order_code_tv.setText(orderBean.getOrderCode());
            int orderStatus=orderBean.getOrderStatus();
            int orderStatusRes;
            switch (orderStatus){
                case 0:
                    orderStatusRes=R.string.orders_unpay;
                    break;
                case 1:
                    orderStatusRes=R.string.orders_payed;
                    break;
                case 2:
                    orderStatusRes=R.string.orders_cancel;
                    break;
                case 3:
                    orderStatusRes=R.string.orders_refund;
                    break;
                case 4:
                    orderStatusRes=R.string.orders_closed;
                    break;
                default:
                    orderStatusRes=R.string.orders_payed;
                    break;
            }
            order_status_tv.setText(orderStatusRes);

            if(orderStatus==3){
                order_refund_status_rl.setVisibility(VISIBLE);
                order_refund_status_line.setVisibility(VISIBLE);
                int refundStatus=orderBean.getRefundStatus();
                int refundStatusRes;
                switch (refundStatus){
                    case 0:
                        refundStatusRes=R.string.orders_refund_status0;
                        break;
                    case 1:
                        refundStatusRes=R.string.orders_refund_status1;
                        break;
                    case 2:
                        refundStatusRes=R.string.orders_refund_status2;
                        break;
                    case 3:
                        refundStatusRes=R.string.orders_refund_status3;
                        break;
                    case 4:
                        refundStatusRes=R.string.orders_refund_status4;
                        break;
                    case 5:
                        refundStatusRes=R.string.orders_refund_status5;
                        break;
                    default:
                        refundStatusRes=R.string.orders_refund_status5;
                        break;
                }
                order_refund_status_tv.setText(refundStatusRes);
            }

            long time = orderBean.getCreateTimeStamp();
            String createTime = formatData(time);
            order_create_time_tv.setText(createTime);
            float order_amount=(float)orderBean.getOrderAmount();
            String amount=cxt.getString(R.string.order_amount_value, order_amount);
            order_amount_tv.setText(amount);
            int receiveType=orderBean.getReceiveType();
            int receiveTypeRes;
            switch (receiveType){
                case 0:
                    receiveTypeRes=R.string.orders_all;
                    break;
                case 1:
                    receiveTypeRes=R.string.orders_eat_in_store;
                    break;
                case 2:
                    receiveTypeRes=R.string.orders_delivery_home;
                    order_delivery_type_hint_tv.setVisibility(VISIBLE);
                    break;
                default:
                    receiveTypeRes=R.string.orders_all;
                    break;
            }
            order_delivery_type_tv.setText(receiveTypeRes);
            ShopBean shopBean=orderBean.getShopBean();
            if(shopBean!=null){
                String shopName=shopBean.getShopName();
                order_relate_store_tv.setText(shopName);
            }

            int isConfirm=orderBean.getIsConfirmed();
            long orderAppointMentTime;
            long orderHandlerTime;
            switch (isConfirm){
                case 0:
                    order_appointment_time_tv.setBackgroundResource(R.drawable.raise_shop_order_item_appointment_time);
                    order_appointment_time_tv.setTextColor(getResources().getColor(R.color.white));
                    order_appointment_time_tv.setText(R.string.orders_appointment_time_unconfirm);
                    order_appointment_time_hint_tv.setVisibility(VISIBLE);
                    break;
                case 1:
                    orderAppointMentTime=orderBean.getAppointTimeStamp();
                    order_appointment_time_tv.setText(timeUtil.formatData4(orderAppointMentTime));
                    break;
                case 2:
                    orderAppointMentTime=orderBean.getAppointTimeStamp();
                    order_appointment_time_tv.setText(timeUtil.formatData4(orderAppointMentTime));
                    orderHandlerTime=orderBean.getBusHandleTimeStamp();
                    order_handler_time_tv.setText(timeUtil.formatData4(orderHandlerTime));
                    order_handler_time_line.setVisibility(VISIBLE);
                    order_handler_time_rl.setVisibility(VISIBLE);
                    break;
                default:
                    break;
            }

            String name=orderBean.getGoodReceiver();
            name_tv.setText(name);
            String phone=orderBean.getReceiverMobile();
            phone_tv.setText(phone);
            String wechatNum=orderBean.getWechatNumber();
            wechate_number_tv.setText(wechatNum);
            String addr=orderBean.getReceiverAddress();
            addr_tv.setText(addr);
            //未处理订单显示标志处理按钮
            if(flag==2){
                bottom_rl.setVisibility(VISIBLE);
            }

        }
    }


    //将时间戳转化为日期格式
    String formatData(long timeStamp) {
        String result = "";
        if (timeUtil.isToday(timeStamp)) {
            result = today + " " + timeUtil.formatHourAndMin(timeStamp);
        } else {
            result = timeUtil.formatData3(timeStamp);
        }
        return result;
    }

    public void setRecyclerItemClick(RecyclerItemClicked clicked) {
        this.itemClicked = clicked;
    }

    @Click({R.id.raise_shop_order_item_unhandler_btn})
    void onClick(View view) {
        switch (view.getId()) {
            case R.id.raise_shop_order_item_unhandler_btn:
                itemClicked.onRecyclerItemClicked(position);
                break;
            default:
                break;
        }

    }
}
